import NProgress from 'nprogress'
import useUserStore from '../store/modules/user'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login']

const permission = async (to, _, next) => {
  NProgress.start()

  const userStore = useUserStore()
  const hasLogin = userStore.token

  if (hasLogin) {
    if (to.path === '/login') next({ path: '/' })
    else next()
  } else {
    if (whiteList.includes(to.path)) next()
    else next('/login')
  }
}

export default permission
